const dbConfig = require('../database/dbconfig')
const jwt = require('jsonwebtoken')
getUserlist = (req, res) => {
    let sql = "select * from user ";
    let sqlArr = [];
    res.header('Access-Control-Allow-Origin', "*");
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    let callBack = (err, sqlResult) => {
        if (err) {
            console.log('sql execute failed, ErrorInfo: ')
        } else {
            res.send(sqlResult);
        }
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
}

getUserInfo = (req, res) => {
    let { id } = req.query;
    let sql = "select * from user where userid=? ";
    let sqlArr = [id];
    res.header('Access-Control-Allow-Origin', "*");
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    let callBack = (err, sqlResult) => {
        if (err) {
            console.log('sql execute failed, ErrorInfo: ')
        } else {
            // console.log(sqlResult);
            res.send({
                'list': sqlResult
            })
        }
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
}


addUser = (req, res) => {
    if (req.body.username === undefined) {
        var postinfo = req.query;
    } else {
        var postinfo = req.body;
    }
    res.header('Access-Control-Allow-Origin', "*");
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    let sql = "insert into user set username=? ,password=? ";
    let sqlArr = [postinfo.username, postinfo.password];
    let callBack = (err, sqlResult) => {
        if (err) {
            console.log('sql execute failed, ErrorInfo: ')
        } else {
            // console.log(sqlResult);
            res.send({
                'info': postinfo,
                'code': 200,
                'msg': 'add user success'
            })
        }
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
}


changePasswd = (req, res) => {
    if (req.body.username === undefined) {
        var postinfo = req.query;
    } else {
        var postinfo = req.body;
    }
    res.header('Access-Control-Allow-Origin', "*");
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    let sql = "update user set password=? where username=? ; ";
    let sqlArr = [postinfo.password, postinfo.username];
    let callBack = (err, sqlResult) => {
        if (err) {
            console.log('sql execute failed, ErrorInfo: ')
        } else {
            // console.log(sqlResult);
            res.send({
                'info': postinfo,
                'code': 200,
                'msg': 'update password success'
            })
        }
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
}


delUser = (req, res) => {
    if (req.body.username === undefined) {
        var postinfo = req.query;
    } else {
        var postinfo = req.body;
    }
    res.header('Access-Control-Allow-Origin', "*");
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    let sql = "delete from  user where username=? ";
    let sqlArr = [postinfo.username];
    let callBack = (err, sqlResult) => {
        if (err) {
            console.log('sql execute failed, ErrorInfo: ')
        } else {
            // console.log(sqlResult);
            res.send({
                'info': postinfo,
                'code': 200,
                'msg': 'update password success'
            })
        }
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
}


userLogin = (req, res) => {
    if (req.body.username === undefined) {
        var postinfo = req.query;
    } else {
        var postinfo = req.body; // mesaage save in body when post
    }
    let sqlArr = [postinfo.username, postinfo.password];

    res.header('Access-Control-Allow-Origin', "*");
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');

    let sql = "select username from user where username=? and password=? ";
    let callBack = (err, sqlResult) => {
        if (err) {
            console.log('sql execute failed, ErrorInfo: ')
        } else {
            res_0 = sqlResult[0]
            if (!res_0) {
                res.send({
                    'info': postinfo,
                    'code': 400,
                    'msg': 'login failed'
                })
            } else {
                // get token       // 注意默认情况 Token 必须以 Bearer+空格 开头
                const token = 'Bearer ' + jwt.sign({
                        _id: 1,
                        admin: true
                    },
                    'secret12345', {
                        expiresIn: 3600 * 24 * 3
                    }
                )
                res.json({
                    'code': 200,
                    'msg': 'login success',
                    data: { token: token }
                })
            }

        }
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
}




userLogout = (req, res) => {
    let { id } = req.query;
    let sql = "select * from user where username=? ";
    let sqlArr = [id];
    res.header('Access-Control-Allow-Origin', "*");
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    let callBack = (err, sqlResult) => {
        if (err) {
            console.log('sql execute failed, ErrorInfo: ')
        } else {
            console.log(length(sqlResult))
            res.send({
                'list': sqlResult
            })
        }
    }
    dbConfig.sqlConnect(sql, sqlArr, callBack)
}


module.exports = {
    getUserlist,
    getUserInfo,
    addUser,
    changePasswd,
    delUser,
    userLogin,
    userLogout,
}